package com.example.myui

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.delay
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.TimeZone

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MaterialTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Greeting("zxy")
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    // 1. 所有图片资源（再次确认：文件名与 res/drawable 目录下完全一致，无大写/特殊符号）
    val backgroundImg = painterResource(R.drawable.erciyuan)
    val phoneImg = painterResource(R.drawable.dianhua)   // 电话图标
    val atImg = painterResource(R.drawable.img2)        // @图标
    val emailImg = painterResource(R.drawable.img3)      // 邮箱图标
    val catImg = painterResource(R.drawable.cat)         // 主内容猫图

    // 时间状态
    var currentTime by remember { mutableStateOf(getBeijingTime()) }
    androidx.compose.runtime.LaunchedEffect(Unit) {
        while (true) {
            delay(1000)
            currentTime = getBeijingTime()
        }
    }

    // 层叠布局：背景 + 内容
    Box(modifier = modifier.fillMaxSize()) {
        // 背景图
        Image(
            painter = backgroundImg,
            contentDescription = "页面背景",
            modifier = Modifier.fillMaxSize(),
            contentScale = ContentScale.Crop,
            alpha = 0.3f
        )

        // 上层内容
        Column(
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.fillMaxSize()
        ) {
            // 时间框
            Box(
                modifier = Modifier
                    .fillMaxWidth(0.7f)
                    .padding(vertical = 16.dp)
                    .clip(RoundedCornerShape(24.dp))
                    .background(Color(0xFFADD8E6).copy(alpha = 0.8f))
                    .padding(vertical = 12.dp, horizontal = 24.dp),
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = currentTime,
                    fontSize = 28.sp,
                    fontWeight = FontWeight.Bold,
                    color = Color(0xFF1E3A5F)
                )
            }

            Spacer(modifier = Modifier.height(32.dp))

            // 主内容区域（猫图能显示，说明这里的Image逻辑没问题）
            Column(
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .fillMaxWidth()
                    .weight(1f)
            ) {
                Image(
                    painter = catImg,
                    contentDescription = "猫图标",
                    modifier = Modifier
                        .size(120.dp)
                        .padding(vertical = 16.dp),
                    contentScale = ContentScale.Fit // 确保图片完整显示
                )
                Text(
                    text = "Hello $name!",
                    textAlign = TextAlign.Center,
                    fontSize = 42.sp,
                    lineHeight = 48.sp,
                    fontWeight = FontWeight.Medium,
                    color = MaterialTheme.colorScheme.primary,
                    modifier = Modifier.padding(vertical = 8.dp)
                )
                Text(
                    text = "Welcome to My App",
                    fontSize = 24.sp,
                    lineHeight = 28.sp,
                    fontWeight = FontWeight.Normal,
                    color = MaterialTheme.colorScheme.secondary,
                    modifier = Modifier.padding(vertical = 8.dp)
                )
            }

            Spacer(modifier = Modifier.height(32.dp))

            // 联系信息区域
            Column(
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 32.dp, vertical = 16.dp)
                    .clip(RoundedCornerShape(16.dp))
                    .background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.6f))
                    .padding(vertical = 16.dp)
            ) {
                // 用 Image 替代 Icon，逻辑和“猫图”完全一致（猫图能显示，这里也该显示）
                ContactRow(img = phoneImg, text = "+86 123 4567 8910")
                Spacer(modifier = Modifier.height(12.dp))
                ContactRow(img = atImg, text = "@zxy")
                Spacer(modifier = Modifier.height(12.dp))
                ContactRow(img = emailImg, text = "zxyqq@qq.com")
            }

            Spacer(modifier = Modifier.height(32.dp))
        }
    }
}

// 关键修改：用 Image 替代 Icon，添加红色背景测试（能看到红色块说明布局在，只是图没显示）
@Composable
fun ContactRow(
    img: androidx.compose.ui.graphics.painter.Painter,
    text: String
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 60.dp),
        horizontalArrangement = Arrangement.Start,
        verticalAlignment = Alignment.CenterVertically
    ) {
        // 用 Image 替代 Icon，添加红色背景（测试用）
        Image(
            painter = img,
            contentDescription = "联系图标",
            modifier = Modifier
                .size(28.dp)
                .alpha(1f) // 完全不透明，排除透明问题
                .background(Color.Red.copy(alpha = 0.3f)), // 红色背景：若看到红色块，说明布局在
            contentScale = ContentScale.Fit, // 确保图片完整显示（避免被裁剪）
            alpha = 1f // 图片本身完全不透明
        )
        Spacer(modifier = Modifier.width(30.dp))
        Text(
            text = text,
            fontSize = 18.sp,
            lineHeight = 20.sp,
            fontWeight = FontWeight.Medium,
            color = Color.Black // 固定黑色，确保文本清晰
        )
    }
}

// 获取北京时间
fun getBeijingTime(): String {
    val sdf = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
    sdf.timeZone = TimeZone.getTimeZone("Asia/Shanghai")
    return sdf.format(Date())
}